package com.rentong.wx.utils;

import com.google.gson.JsonObject;
import com.google.gson.JsonParser;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.util.EntityUtils;

public class TencentMapReverseGeocoding {

    private static final String API_KEY = "HNHBZ-2UQKW-JSDRM-3ZF42-5TJJQ-3SFXN"; // 替换为你的API Key
    private static final String BASE_URL = "https://apis.map.qq.com/ws/geocoder/v1/";

//public static void main(String[] args) {
//    System.out.println(getAMapByLngAndLat( "118.823040", "32.035849"));
//}


    public static String getCity(String longitude, String latitude) {
        Double lat = Double.parseDouble(latitude);
        Double lon = Double.parseDouble(longitude);

        String location = String.format("%f,%f", lat, lon); // 格式化为腾讯地图所需的格式
        String url = BASE_URL + "?location=" + location + "&key=" + API_KEY;

        try (CloseableHttpClient client = HttpClients.createDefault()) {
            HttpGet request = new HttpGet(url);
            String responseBody = EntityUtils.toString(client.execute(request).getEntity());
            System.out.println(responseBody);
            return parseResponse(responseBody);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return "";
    }

    public static String getAMapByLngAndLat(String longitude, String latitude) {
        Double lat = Double.parseDouble(latitude);
        Double lon = Double.parseDouble(longitude);

        String location = String.format("%f,%f", lat, lon); // 格式化为腾讯地图所需的格式
        String url = BASE_URL + "?location=" + location + "&key=" + API_KEY;

        try (CloseableHttpClient client = HttpClients.createDefault()) {
            HttpGet request = new HttpGet(url);
            String responseBody = EntityUtils.toString(client.execute(request).getEntity());
            return parseResponse2(responseBody);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return "";
    }

    //返回城市
    private static String parseResponse(String response) {
        JsonParser parser = new JsonParser();
        JsonObject jsonObject = parser.parse(response).getAsJsonObject();
        if (jsonObject.has("result") && jsonObject.get("result").isJsonObject()) {
            JsonObject result = jsonObject.getAsJsonObject("result");
            if (result.has("address")) {
//                System.out.println("Address: " + result.get("address").getAsString());
//                System.out.println("address_component: " + result.get("address_component").getAsJsonObject().get("city"));
                System.out.println(result.get("address_component").getAsJsonObject().get("city").getAsString());
                return result.get("address_component").getAsJsonObject().get("city").getAsString();
            } else {
                System.out.println("No address found.");
            }
        } else {
            System.out.println("No result found.");
        }
        return "";
    }

    //返回城市
    private static String parseResponse2(String response) {
        JsonParser parser = new JsonParser();
        JsonObject jsonObject = parser.parse(response).getAsJsonObject();
        if (jsonObject.has("result") && jsonObject.get("result").isJsonObject()) {
            JsonObject result = jsonObject.getAsJsonObject("result");
            if (result.has("address")) {
                return result.get("address").getAsString();
            } else {
                System.out.println("No address found.");
            }
        } else {
            System.out.println("No result found.");
        }
        return "";
    }


}
